#include <stdio.h>
#include <sys/shm.h>
#include <unistd.h>
#include <string.h>
 
int main(int argc, char **argv) {
	int shmid;
	int i = 0;
	char *pshm;
	char buf[1024];
	//创建一块共享内存:1000:起始地址,size:10240
	shmid = shmget(1000, 10240, 0666 | IPC_CREAT);
	if(shmid < 0 )
		fprintf(stderr, "create share memory error\n");
	
	//pshm指针指向内存区域(0,0)
	pshm = (char*)shmat(shmid, 0, 0);
	if(pshm == NULL)
		fprintf(stderr, "attach shm error\n");
 
	printf("input node 0-9\n");
	scanf("%d", &i);
	printf("node is %d\n", i);
 
	memset(buf, 0, sizeof(buf));
	printf("input data\n");
	scanf("%s", buf);
	
	//向共享内存写buf数据
	memcpy(pshm + i*20, buf, 20);
 
	//指针解除绑定，进程结束系统会释放共享内存对物理地址得映射
	shmdt(pshm);
	return 0;
}